package com.lagou.edu.controller;

import com.lagou.edu.service.LagouEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/email")
public class LagouEmailController {

    @Autowired
    private LagouEmailService emailService;

    @Value("${config.email.templates.checkcode.title}")
    private String emailTitle;

    @Value("${config.email.templates.checkcode.content}")
    private String emailContent;


    /**
     * 发送验证码到邮箱
     * @param email
     * @param code
     * @return
     */
    @GetMapping("/sendEmail/{email}/{code}")
    public boolean sendEmail(@PathVariable("email") String email, @PathVariable("code") String code){
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(code)){
            return false;
        }else {
            String message = String.format(emailContent, code);
            return emailService.sendEmail(email, emailTitle, message);
        }
    }

}
